var gulp = require('gulp');
var rev = require('gulp-rev');
var revReplce = require('gulp-rev-replace');
var clean = require('gulp-clean');
var jeditor = require('gulp-json-editor'); 
var sass = require('gulp-sass');
var csso = require('gulp-csso');
var autoprefixer = require('gulp-autoprefixer');
var htmlmin = require('gulp-htmlmin');
var uglify = require('gulp-uglify');
var gulpif = require('gulp-if');

var htmlminopt = {
	removeComments: true,//清除HTML注释
    collapseWhitespace: true,//压缩HTML
    collapseBooleanAttributes: false,//省略布尔属性的值 <input checked="true"/> ==> <input />
    removeEmptyAttributes: false,//删除所有空格作属性值 <input id="" /> ==> <input />
    removeScriptTypeAttributes: false,//删除<script>的type="text/javascript"
    removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
    minifyJS: true,//压缩页面JS
    minifyCSS: true//压缩页面CSs
};

// 编译sass文件
gulp.task('sass', function() {
	gulp.src(['static/sass/*.scss'], {base: 'static/sass'})
		.pipe(sass({
        	outputStyle: 'compressed'           // 配置输出方式,默认为nested
    	}))
    	.pipe(autoprefixer({
	        browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8', 'iOS >= 8', 'Android >= 4'],
	        cascade: true,                     // 美化属性，默认true
	        add: true,                           // 是否添加前缀，默认true
	        remove: true,                        // 删除过时前缀，默认true
	        flexbox: true                       // 为flexbox属性添加前缀，默认true
	    }))
	    .pipe(gulp.dest('static/css'));
});

gulp.watch('static/sass/*.scss', ['sass']);

// 删除生成在dist目录下的文件
gulp.task('del', function() {
	del('dist');
	del('appliction/dist');
})

gulp.task('clean', function() {
	return gulp.src([
		'dist/**/**/*.js',
		'appliction/dist/**/**/*.php'
		])
		.pipe(clean({force: true}));
});

// 拷贝无需处理的php文件到dist文件夹中
gulp.task('copyphp', ['clean'], function() {
	return gulp.src([
		'application/views/**/*.php',
		'!application/views/Admin/*.php'
		], {base: 'application/views'})
		.pipe(gulp.dest('application/dist'))
});

function cssCondition(name) { 
	return /\.css$/.test(name.path);
}

function jsCondition(name) {
	return /\.js$/.test(name.path);
}

gulp.task('revision', ['copyphp'], function() {
	return gulp.src([
		'static/js/**/*.js', 
		'static/css/**/*.css'
		], {base: './static'}) 
		.pipe(gulpif(jsCondition, uglify()))
		.pipe(rev())
		.pipe(gulp.dest('dist'))
		.pipe(rev.manifest())
		.pipe(jeditor(function(json) {
			var newJson = {};
			for (var key in json) {
				newJson['/static/' + key] = '/dist/' + json[key];  // 修改生成的manifest.json文件
			}
			return newJson;
		}))
		.pipe(gulp.dest('dist'));
});


gulp.task('revplace', ['revision'], function() {
	var manifest = gulp.src('dist/rev-manifest.json'); 

	return gulp.src('application/views/Admin/**/*.php', {base: 'application/views'})
		.pipe(revReplce({
			manifest: manifest,
			canonicalUris: false,
			// prefix: '/dist/',
			replaceInExtensions: ['.js', '.css', '.html', '.hbs', '.php']
		}))
		.pipe(htmlmin(htmlminopt))
		.pipe(gulp.dest('application/dist'));
});
 

